<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #outer{
            width: 500px;
            margin: 50px auto;
            padding: 10ox;
            background: greenyellow;
            text-align: center;
        }
        img{
            width: 500px;
            height: 350px;
        }
    </style>

    <script>
        window.onload = function(){
            /*
            点击按钮切换图片
            */
            //获取两个按钮
            var prev = document.getElementById("prev");
            var next = document.getElementById("next");
            
            //切换图片就是修改img中的src属性

            //获取img
            var img = document.getElementsByTagName("img")[0];

            //创建一个数组，用来保存路径
            // var imgArr = ["images/01_日落.jpg","images/02_日落.jpg",
            // "images/03_日落.jpg","images/04_日落.jpg",
            // "images/05_日落.jpg"            
            // ];

            var imgArr = ["images/01_日落.jpg","images/02_日落.jpg","images/03_日落.jpg","images/04_日落.jpg","images/05_日落.jpg"];
            
            //创建一个变量保存当前正在显示图片的索引
            var index = 0;

            //获取id为info的p元素
            var info = document.getElementById("info");
            //设置提示文字
            info.innerHTML = "一共"+imgArr.length+"张图，当前为第"+(index+1)+"张";
            

            //分别为两个按钮单击响应函数
            prev.onclick = function(){ 
                index--;
                if(index<0){
                    index = imgArr.length-1;
                }

                img.src = imgArr[index];
                info.innerHTML = "一共"+imgArr.length+"张图，当前为第"+(index+1)+"张";

            };

            next.onclick = function(){
                index++;
                if( index > imgArr.length-1){
                    index = 0;
                    
                }
                img.src = imgArr[index];
                info.innerHTML = "一共"+imgArr.length+"张图，当前为第"+(index+1)+"张";

                //切换属性就是改属性
                //要修改一个元素的属性，属性=属性值



            };

        }

    </script>    

</head>
<body>
    <div id="outer">
        <p id="info"></p>
        <img src="images/01_日落.jpg" alt="">

        <button id="prev">上一张</button>
        <button id="next">下一张</button>
    </div>
</body>
</html>